package com.qin.controller;

import com.alibaba.fastjson.JSONObject;
import com.qin.entity.Environment;
import com.qin.enums.RetCode;
import com.qin.service.EnvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;

@Controller("envController")
@RequestMapping("/env")
@ResponseBody
public class EnvController {

    @Autowired
    private EnvService envService;

    @RequestMapping(value="/queryEnvironments", consumes="application/json;charset=UTF-8", produces="application/json;charset=UTF-8")
    public String queryEnvironments(@RequestBody String body){
//        System.out.println("web层：queryEnvironments");
        System.out.println("接口调用：/env/queryEnvironments");

        //入参封装到实体类
        Environment environment = JSONObject.parseObject(body, Environment.class);
        System.out.println("接口入参：" + body);
        //调用service层
        String retString = envService.queryEnvironments(environment).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/insertEnvironment", produces="text/html;charset=UTF-8")
    public String insertEnvironment(@RequestBody String body){
//        System.out.println("web层：insertEnvironment");
        System.out.println("接口调用：/env/insertEnvironment");

        //入参封装到实体类
        Environment environment = JSONObject.parseObject(body, Environment.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((environment.getSystem_name() == null) || ("".equals(environment.getSystem_name()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "system_name不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }
        else if((environment.getEnvironment_stg() == null) || ("".equals(environment.getEnvironment_stg()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "environment_stg不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }
        else if((environment.getSystem_url() == null) || ("".equals(environment.getSystem_url()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "system_url不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = envService.insertEnvironment(environment).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/deleteEnvironment", produces="text/html;charset=UTF-8")
    public String deleteEnvironment(@RequestBody String body){
//        System.out.println("web层：deleteEnvironment");
        System.out.println("接口调用：/env/deleteEnvironment");

        //入参封装到实体类
        Environment environment = JSONObject.parseObject(body, Environment.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((environment.getId() == null) || ("".equals(environment.getId()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = envService.deleteEnvironment(environment).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/updateEnvironment", produces="text/html;charset=UTF-8")
    public String updateEnvironment(@RequestBody String body){
//        System.out.println("web层：updateEnvironment");
        System.out.println("接口调用：/env/updateEnvironment");

        //入参封装到实体类
        Environment environment = JSONObject.parseObject(body, Environment.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((environment.getId() == null) || ("".equals(environment.getId()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = envService.updateEnvironment(environment).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }
}
